aboutsummaryrefslogtreecommitdiff
path: root/src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts')
-rw-r--r--src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts b/src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts
new file mode 100644
index 0000000..83bac3c
--- /dev/null
+++ b/src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts
@@ -0,0 +1,80 @@
+import { error, fail } from '@sveltejs/kit';
+import { getById } from '$lib/server/services/projects';
+import {
+ edit,
+ complete,
+ loadCurrent,
+ loadCompletions,
+} from '$lib/server/services/routines';
+import { numericId, parseFormString } from '$lib/server/services/validation';
+import {
+ parseIntervalToDays,
+ computeRoutineState,
+} from '$lib/shared/routine-metrics';
+import type { Actions, PageServerLoad } from './$types';
+
+function parseFrequencyDays(value: FormDataEntryValue | null): number {
+ const days = parseIntervalToDays(value?.toString() ?? '');
+ if (days === null) {
+ throw new Error('Invalid interval');
+ }
+ return days;
+}
+
+export const load: PageServerLoad = async ({ params }) => {
+ const projectId = numericId(params.projectId);
+ const routineId = numericId(params.routineId);
+
+ const project = await getById(projectId);
+ if (!project || project.archived) {
+ throw error(404, 'Projet introuvable');
+ }
+
+ const routine = await loadCurrent(routineId);
+ if (!routine || routine.archived || routine.projectId !== projectId) {
+ throw error(404, 'Routine introuvable');
+ }
+
+ const completions = await loadCompletions(routineId);
+ return {
+ project: { id: project.id, name: project.name },
+ routine,
+ completions,
+ state: computeRoutineState(routine, completions, Date.now()),
+ };
+};
+
+export const actions: Actions = {
+ edit: async ({ request, params }) => {
+ const data = await request.formData();
+ const name = parseFormString(data.get('name'));
+ if (!name) {
+ return fail(400, { error: 'Le nom est requis' });
+ }
+ const description = data.get('description')?.toString() ?? '';
+ try {
+ await edit(numericId(params.routineId), {
+ name,
+ frequencyDays: parseFrequencyDays(data.get('frequencyDays')),
+ description,
+ });
+ } catch {
+ return fail(400, { error: 'Fréquence invalide' });
+ }
+ return { success: true, type: 'edit' as const };
+ },
+
+ complete: async ({ request, params }) => {
+ const data = await request.formData();
+ try {
+ await complete(
+ numericId(params.routineId),
+ parseFormString(data.get('note')),
+ new Date().toISOString(),
+ );
+ } catch {
+ return fail(400, { error: 'Routine introuvable' });
+ }
+ return { success: true };
+ },
+};